<?php

namespace app\index\controller;

use app\lib\enum\RecommendStatus;
use app\lib\enum\Status;

class Index extends BaseController
{
    //首页
    public function index(){


        //流程：判断是否传递cityId过来
        //通过重定向到handle页面，保证每次到这个首页上的url都死包含了cityId的
        //如果没有，则加载handle.html页面
        //如果有，则可根据该cityId，查找条件数据

        $cityId=input('get.cityId');
        //使用session和localStorage联合判断用户是否选择城市
        if(!$cityId){
            if(!session("cityId")){
                return view("handle");
            }else{
                $cityId=session("cityId");
            }
        }

        //将cityId，记录到session中，多界面共享
        //在取值时，首先从session中取值
        session("cityId",$cityId);


        //轮播图片地址
        $carouselList=model("PyRecommend")->where("rec_role","=",RecommendStatus::Carousel)->select();
        foreach ($carouselList as &$value){
            $value['rec_url']=$value['rec_url']!=""?$value['rec_url']:"#";
        }
        $this->assign("carouselList",$carouselList);


        //机构和文章的数量信息
        $institutionNum=count(model("PyInstitution")->select());
        $articleNum=count(model("CmsArticle")->select());
        $this->assign("institutionNum",$institutionNum);
        $this->assign("articleNum",$articleNum);

        //平台推荐
        $recommendList=model("PyRecommend")->where("rec_role","=",RecommendStatus::Recommend)
            ->order("rec_order","asc")
            ->limit(0,config("setting.indexRecommendNum"))->select();
        foreach ($recommendList as &$value){
            $value['rec_url']=$value['rec_url']!=""?$value['rec_url']:"#";
        }
        $this->assign("recommendList",$recommendList);


        //好课 列表（注意要使用）
        $goodClassCondition=[];
        $goodClassCondition['art_isshow']=['eq',Status::on];
        $goodClassCondition['art_ischeck']=['eq',Status::on];
        $goodClassCondition['art_category_id']=["in",getGoodClassSubCategoryIds()];
        $goodClassCondition['art_expire_time']=['>',time()]; //不显示过期好课
        $goodClassCondition['art_ishot']=['eq',Status::on];  //只显示热门标记的好课
        $tempList=model("CmsArticle")->with("bindInstitution,bindCategory")
            ->where($goodClassCondition)
            ->order("art_sort","asc")
            ->limit(0,config("setting.goodClassNum"))
            ->select();


        //筛选出属于指定的城市的好课信息
        //同时也要筛选出机构未被禁用的文章
        $goodClassList=[];
        foreach ($tempList as &$value){

            if($value['bind_institution']['ins_city_id']==$cityId&&$value['bind_institution']['ins_status']==Status::on){
                $goodClassList[]=$value;
            }
        }


        //处理信息
        foreach ($goodClassList as &$value){
            $value['art_create_time']=date("Y-m-d",$value['art_create_time']);
            $value['art_url']=url("index/Article/goodClassDetail")."?art_id=".$value['art_id'];
        }
        $this->assign("goodClassList",$goodClassList);



        //好文精选列表
        //展示条件（1. 热门文章；2. 新文章先显示即时间降序显示；3. 只显示指定的条数）
        $goodArticleCondition=[];
        $goodArticleCondition['art_isshow']=['eq',Status::on];
        $goodArticleCondition['art_ischeck']=['eq',Status::on];
        $goodArticleCondition['art_ishot']=['eq',Status::on];   //首页仅仅显示热门文章
        $goodArticleCondition['art_category_id']=['in',getGoodArticleSubCategoryIds()];
        $goodArticleCondition['art_expire_time']=['>',time()]; //不显示过期文章
        $goodArticleList=model("CmsArticle")->where($goodArticleCondition)
            ->order("art_id","desc")
            ->limit(0,config("setting.goodArticleNum"))
            ->select();
        foreach ($goodArticleList as &$value){
            $value['art_create_time']=date("Y-m-d",$value['art_create_time']);
//            {:url('index/Article/goodArticleDetail')}?art_id={$vo['art_id']}
            $value['art_url']=url("index/Article/goodArticleDetail")."?art_id=".$value['art_id'];
        }
        $this->assign("goodArticleList",$goodArticleList);

        //地址
        $cityId=input("get.cityId");
        $cty_name="暂未选择";
        if($cityId!=""){
            $cityInfo=model("PyCity")->find($cityId);
            $cty_name=$cityInfo["cty_name"];
        }
        $this->assign("cty_name",$cty_name);


        //判断是否有cityId传到页面上


        return view();
    }

    //地址选择
    public function chooseLocation(){
        $cityList=model("PyCity")->select();
        $this->assign("cityList",$cityList);
        return view();
    }

    //根据传入的城市信息
    public function getCityInfo(){
        $cityId=input("post.cityId");
        $cityInfo=model("PyCity")->find($cityId);
        $this->success("","",$cityInfo);
    }


    public function chooseCategory(){
        $ins_id=input("get.ins_id");
        $this->assign("ins_id",$ins_id);
        $categoryList=model("CmsArticleCategory")->where("acc_id","in",getGoodClassSubCategoryIds())->select();
        $this->assign("categoryList",$categoryList);
        return view();
    }

    public function chooseGoodArticleCategory(){
        $categoryList=model("CmsArticleCategory")->where("acc_id","in",getGoodArticleSubCategoryIds())->select();
        $this->assign("categoryList",$categoryList);
        return view();
    }
}
